package gr.teiath.newsfeed;

import android.app.Activity;
import android.content.Intent;
import android.database.Cursor;
import android.os.AsyncTask;
import android.os.Bundle;
import android.util.Log;
import android.widget.ListView;
import android.widget.Toast;

public class FacultyList extends Activity{
	
	private static final String tag = FacultyList.class.getSimpleName();
	
	private Cursor cursor;
	private NewsfeedApplication newsfeed;
	private ListView facultyList;
	private String group_id;
	private static final String[] FROM = { "name" };
	private static final int[] TO = { R.id.facultyTxtView };
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.facultylist);
		
		Intent prefIntent = getIntent();
		group_id = prefIntent.getStringExtra("preference");
		group_id = group_id.substring((group_id.length() - 1));
		
		newsfeed = (NewsfeedApplication) getApplication();
		new readFacultiesTask().execute(group_id);
		
		Log.i(tag, "****** FacultyList // onCreate");
	}
	
	protected void onRestart() {
		super.onRestart();
		new readFacultiesTask().execute();
		
		Log.i(tag, "****** FacultyList // onRestart");
	}

	@Override
	protected void onDestroy() {
		super.onDestroy();
	}
	
	private class readFacultiesTask extends AsyncTask<String, Integer, Cursor> {

		@Override
		protected Cursor doInBackground(String... group_id) {
			cursor = newsfeed.readFaculties(group_id, true);
			
			Log.i(tag, "****** FacultyList // readFacultiesTask()");
			
			return cursor;
		}

		@Override
		protected void onPostExecute(Cursor cursor) {
			startManagingCursor(cursor);			

			facultyList = (ListView) findViewById(R.id.facultyList);
			//SimpleCursorAdapter adapter = new SimpleCursorAdapter(FacultyList.this, R.layout.facultyrow, cursor, FacultyList.FROM, FacultyList.TO);
			FacultyAdapter adapter = new FacultyAdapter(FacultyList.this, R.layout.facultyrow, cursor, FacultyList.FROM, FacultyList.TO);
			facultyList.setAdapter(adapter);
		}

		@Override
		protected void onProgressUpdate(Integer... values) {
			super.onProgressUpdate(values);
			Toast.makeText(FacultyList.this, "readFacultiesTask() running...", Toast.LENGTH_SHORT).show();
		}
		
		
	}

}
